package cn.jekun.shop.app.action;

import javax.annotation.Resource;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import cn.jekun.component.base.bean.ErrorMsg;
import cn.jekun.component.base.exceptions.BusinessException;
import cn.jekun.shop.common.service.ShopGoodService;
/**
 * 商家商品管理
 * @author yuan
 *
 */
@SuppressWarnings("serial")
public class ShopGoodAction extends AppBaseAction{
	private static Logger logger=Logger.getLogger(ShopGoodAction.class);
	
	@Resource(name=ShopGoodService.BEAN_NAME)
	ShopGoodService shopGoodService;
	
	/**
	 * 商家商品管理的商品列表接口
	 * @return
	 */
	public String goodList(){
		JSONObject jsonObject = null;
		try{
			String shopId = this.request.getParameter("shopId");//商家代码
			String typeId = this.request.getParameter("typeId");//商家自定义商品分类 0 全部
			String online = this.request.getParameter("online");//筛选(2全部、1上架、0下架、3有图、4无图、5推荐、6非推荐)
			String sort = this.request.getParameter("sort");//排序 0全部、 1销量最高、2销量最低、3格最高、4价格最低
			String pageSize = this.request.getParameter("pageSize");
			String currentPage = this.request.getParameter("currentPage");
			
			try {
				this.page.setPageSize(Integer.parseInt(pageSize));
			} catch (Exception e) {
				this.page.setPageSize(10);
			}
			try {
				this.page.setPageNumber(Integer.parseInt(currentPage));
			} catch (Exception e) {
				this.page.setPageNumber(1);
			}
			
			this.validateLoginApp();
			this.validateSign(shopId,typeId,online,sort);
			
			jsonObject = this.shopGoodService.goodList(shopId,typeId,online,sort,page);
		} catch (BusinessException e) {
			// TODO: handle exception
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}
	/**
	 * 商家商品管理的搜索商品接口
	 * @return
	 */
	public String searchGoods(){
		JSONObject jsonObject = null;
		try{
			String shopId = this.request.getParameter("shopId");//商家代码
			String name = this.request.getParameter("name");//商品名称
			String pageSize = this.request.getParameter("pageSize");
			String currentPage = this.request.getParameter("currentPage");
			String barCode = this.request.getParameter("barCode");
			
			try {
				this.page.setPageSize(Integer.parseInt(pageSize));
			} catch (Exception e) {
				this.page.setPageSize(10);
			}
			try {
				this.page.setPageNumber(Integer.parseInt(currentPage));
			} catch (Exception e) {
				this.page.setPageNumber(1);
			}
			
			this.validateLoginApp();
			this.validateSign(shopId,name);
			
			jsonObject = this.shopGoodService.searchGoods(shopId,name,page,barCode);
		} catch (BusinessException e) {
			// TODO: handle exception
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}
	
	/**
	 * 关键词搜索接口
	 * @return
	 */
	public String searchByHot(){
		JSONObject jsonObject = null;
		try{
			String pageSize = this.request.getParameter("pageSize");
			String currentPage = this.request.getParameter("currentPage");
			
			try {
				this.page.setPageSize(Integer.parseInt(pageSize));
			} catch (Exception e) {
				this.page.setPageSize(10);
			}
			try {
				this.page.setPageNumber(Integer.parseInt(currentPage));
			} catch (Exception e) {
				this.page.setPageNumber(1);
			}
			
//			this.validateLoginApp();
			this.validateSign(pageSize,currentPage);
			
			jsonObject = this.shopGoodService.searchByHot(page);
		} catch (BusinessException e) {
			// TODO: handle exception
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}
	
	/**
	 * 商家商品管理的商品上下架设置接口
	 * @return
	 */
	public String online(){
		JSONObject jsonObject = null;
		try{
			String merchandiseId = this.request.getParameter("merchandiseId");//商品代码
			String online = this.request.getParameter("online");//全部上架下架(2全部、1上架、0下架)
			
			this.validateLoginApp();
			this.validateSign(merchandiseId,online);
			
			jsonObject = this.shopGoodService.online(getUserId(),merchandiseId, online);
			
			this.logService.addMallLog(this.getUserId(),this.getBrowser(), this.getIp(), "手机商家设置商品"+merchandiseId+"上下架"+online);//记录操作日志
			
		} catch (BusinessException e) {
			// TODO: handle exception
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}
	
	/**
	 * 商家商品管理的商品推荐切换设置接口
	 * @return
	 */
	public String recommended(){
		JSONObject jsonObject = null;
		try{
			String merchandiseId = this.request.getParameter("merchandiseId");//商品代码
			String recommended = this.request.getParameter("recommended");//推荐(1是0否)
			
			this.validateLoginApp();
			this.validateSign(merchandiseId,recommended);
			
			jsonObject = this.shopGoodService.recommended(getUserId(),merchandiseId, recommended);
			
			this.logService.addMallLog(this.getUserId(),this.getBrowser(), this.getIp(), "手机商家设置商品"+merchandiseId+"是否推荐"+recommended);//记录操作日志
			
		} catch (BusinessException e) {
			// TODO: handle exception
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}
	
	
	/**
	 * 商家商品管理的商品信息
	 * @return
	 */
	public String getGoodDetail(){
		JSONObject jsonObject = null;
		try{
			String merchandiseId = this.request.getParameter("merchandiseId");//商品代码
			
//			this.validateLoginApp();
//			this.validateSign(merchandiseId);
			
			jsonObject = this.shopGoodService.getGoodDetail(merchandiseId);
		} catch (BusinessException e) {
			// TODO: handle exception
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}
	
	/**
	 * 商家商品管理的扫描条码时获取商品信息
	 * @return
	 */
	public String getGoodDetailByBarcode(){
		JSONObject jsonObject = null;
		try{
			String shopId = this.request.getParameter("shopId");//商家代码
			String barcode = this.request.getParameter("barcode");//条形码
			
			this.validateLoginApp();
			this.validateSign(shopId,barcode);
			
			jsonObject = this.shopGoodService.getGoodDetailByBarcode(shopId,barcode);
		} catch (BusinessException e) {
			// TODO: handle exception
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
			e.printStackTrace();
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}
	
	/**
	 * 商家商品管理的增加商品接口
	 * @return
	 */
	public String addGood(){
		JSONObject jsonObject = null;
		try{
			String shopId = this.request.getParameter("shopId");//商家代码
			String merchandiseTypeId = this.request.getParameter("merchandiseTypeId");//系统分类
			String shopMerchandiseTypeId = this.request.getParameter("shopMerchandiseTypeId");//商家自定义分类
			String merchandiseName = this.request.getParameter("merchandiseName");//商品名称
			String price = this.request.getParameter("price");//销售价
			String mktprice = this.request.getParameter("mktprice");//市场价
			String cost = this.request.getParameter("cost");//成本价
			String inventory = this.request.getParameter("inventory");//库存
			String brief = this.request.getParameter("brief");//简介
			String barcode = this.request.getParameter("barcode");//条形码
			String closed = this.request.getParameter("closed");//(1上架、0下架)
			String merchandiseWeight = this.request.getParameter("merchandiseWeight");//权重
			String recommended = this.request.getParameter("recommended");//推荐(1是0否)

			String imageUrl = this.request.getParameter("imageUrl");//图片地址(多个用逗号分开)
			String unitName = this.request.getParameter("unitName");//单位
			this.validateLoginApp();
			this.validateSign(shopId,merchandiseTypeId,shopMerchandiseTypeId,merchandiseName,price,mktprice,cost,inventory,barcode);
			
			jsonObject = this.shopGoodService.addGood(shopId,merchandiseTypeId,shopMerchandiseTypeId,merchandiseName,price,mktprice,cost,inventory,brief,closed,barcode,imageUrl,getUserId(),merchandiseWeight,unitName,recommended);
			
			this.logService.addMallLog(this.getUserId(),this.getBrowser(), this.getIp(), "手机商家"+shopId+"增加商品"+merchandiseName);//记录操作日志
		} catch (BusinessException e) {
			// TODO: handle exception
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}
	
	/**
	 * 商家商品管理的保存修改商品接口
	 * @return
	 */
	public String editGood(){
		JSONObject jsonObject = null;
		try{
			String shopId = this.request.getParameter("shopId");//商家代码
			String merchandiseId = this.request.getParameter("merchandiseId");//商品代码
			String merchandiseTypeId = this.request.getParameter("merchandiseTypeId");//系统分类
			String shopMerchandiseTypeId = this.request.getParameter("shopMerchandiseTypeId");//商家自定义分类
			String merchandiseName = this.request.getParameter("merchandiseName");//商品名称
			String price = this.request.getParameter("price");//销售价
			String mktprice = this.request.getParameter("mktprice");//市场价
			String cost = this.request.getParameter("cost");//成本价
			String inventory = this.request.getParameter("inventory");//库存
			String brief = this.request.getParameter("brief");//简介
			String barcode = this.request.getParameter("barcode");//条形码
			String closed = this.request.getParameter("closed");//(1上架、0下架)
			String merchandiseWeight = this.request.getParameter("merchandiseWeight");//权重
			String unitName = this.request.getParameter("unitName");//单位
			String imageUrl = this.request.getParameter("imageUrl");//图片地址(多个用逗号分开)
			String recommended = this.request.getParameter("recommended");//推荐(1是0否)

			this.validateLoginApp();
			this.validateSign(shopId,merchandiseId,merchandiseTypeId,shopMerchandiseTypeId,merchandiseName,price,mktprice,cost,inventory,barcode);
			
			jsonObject = this.shopGoodService.editGood(shopId,merchandiseId,merchandiseTypeId,shopMerchandiseTypeId,merchandiseName,price,mktprice,cost,inventory,brief,closed,barcode,imageUrl,getUserId(),merchandiseWeight,unitName,recommended);
			
			this.logService.addMallLog(this.getUserId(),this.getBrowser(), this.getIp(), "手机商家"+shopId+"修改商品"+merchandiseName);//记录操作日志
		} catch (BusinessException e) {
			// TODO: handle exception
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}
	
	/**
	 * 商家商品管理的增加商家自定义分类
	 * @return
	 */
	public String addType(){
		JSONObject jsonObject = null;
		try{
			String shopId = this.request.getParameter("shopId");//商家代码
			String parentId = this.request.getParameter("parentId");//上级商品分类代码
			String typeName = this.request.getParameter("typeName");//商品分类名称

			this.validateLoginApp();
			this.validateSign(shopId,parentId,typeName);
			
			jsonObject = this.shopGoodService.addType(shopId,parentId,typeName,getUserId());
			
			this.logService.addMallLog(this.getUserId(),this.getBrowser(), this.getIp(), "手机商家"+shopId+"增加商品分类"+typeName);//记录操作日志
		} catch (BusinessException e) {
			// TODO: handle exception
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}
	/**
	 * 商家商品管理的修改商家自定义分类
	 * @return
	 */
	public String editType(){
		JSONObject jsonObject = null;
		try{
			String typeId = this.request.getParameter("typeId");//商品分类代码
			String typeName = this.request.getParameter("typeName");//商品分类名称

			this.validateLoginApp();
			this.validateSign(typeId,typeName);
			
			jsonObject = this.shopGoodService.editType(typeId,typeName,getUserId());
			
			this.logService.addMallLog(this.getUserId(),this.getBrowser(), this.getIp(), "手机商家修改商品分类"+typeId+","+typeName);//记录操作日志
			
		} catch (BusinessException e) {
			// TODO: handle exception
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}
  }
